package com.zt.rongda.employee.controller;

import com.zt.servlet.*;
import com.zt.rongda.employee.model.EmpContract;
import com.zt.rongda.employee.service.EmpContractService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/empContract")
public class EmpContractController {

    @Autowired
    private EmpContractService empContractService;

    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(empContractService.getTable(reqBean));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        empContractService.add(getEmpContract(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        empContractService.update(getEmpContract(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        empContractService.delete(reqBean.getInfo("contId"));
        return Response.responseSuccess();
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private EmpContract getEmpContract(ReqBean reqBean) {
        EmpContract empContract = new EmpContract();
        empContract.setContId(reqBean.getInfo("contId"));
        empContract.setEmpId(reqBean.getInfo("empId"));
        empContract.setEmpName(reqBean.getInfo("empName"));
        empContract.setDeptId(reqBean.getInfo("deptId"));
        empContract.setDeptName(reqBean.getInfo("deptName"));
        empContract.setPosId(reqBean.getInfo("posId"));
        empContract.setPosName(reqBean.getInfo("posName"));
        empContract.setType(reqBean.getInfo("type"));
        empContract.setTerm(reqBean.getInfoInt("term"));
        empContract.setStartdate(reqBean.getInfoDate("startdate"));
        empContract.setEnddate(reqBean.getInfoDate("enddate"));
        empContract.setFiredate(reqBean.getInfoDate("firedate"));
        empContract.setFirereason(reqBean.getInfo("firereason"));
        empContract.setStatus(reqBean.getInfo("status"));
        empContract.setPhoto(reqBean.getInfo("photo"));
        empContract.setOperator(reqBean.getUserId());
        empContract.setRemark(reqBean.getInfo("remark"));
        return empContract;
    }

}